/* 图片上传 */
var $picBorderRaw = $('#picBorderRaw').html();
/**
 * 增加图片框
 */
var delComfirm = '确定删除？';
$('.pic_border .del').live('mouseout', function () {
    if (delComfirm == this.innerHTML) {
        this.innerHTML = $(this).attr('oriHTML');
    }
});

window.pic = {
    replaceRaw: function () {
        return $picBorderRaw.replace(/__picType__/g)
            .replace('__maxFileID__', max_file_id++);
    },
    /**
     * 增加
     */
    add: function () {
        $(".picarea").append(this.replaceRaw());
    },
    /**
     * 在某个图片前面增加
     */
    addBefore: function (_this) {
        $(_this).parent().parent().before(this.replaceRaw());
    },

    delThis: function (_this) {
        $(_this).parent().parent().remove();
    },
    /**
     * 删除图片
     */
    del: function (_this, act) {
        if (delComfirm == $(_this).html()) {
            var imgsrc = $(".thumbnail input[name='pic[]']]").val();
            if (imgsrc == '') {
                imgsrc = $(".thumbnail img").attr('src');
                imgsrc = imgsrc.split(act);
                imgsrc = imgsrc[1];
            }
            $.ajax({
                url: '/file/deletePic',
                type: 'GET',
                data: {src: imgsrc, act: act},
                success: function (json) {
                    $(".thumbnail input[name='pic[]']]").val('');
                    $(".thumbnail img").attr('src', '');
                    $(".thumbnail span").text('');
                }
            });

        } else {
            $(_this).attr('oriHTML', $(_this).html());
            $(_this).html(delComfirm);

        }
    },
    /**
     * 左移
     */
    moveleft: function (_this) {
        var $parent = $(_this).parent().parent();
        if (0 == $parent.prev('.picupload').length) {
            art.dialog.tips('已经是第一张！', 1);
            return;
        }
        $parent.clone().insertBefore($parent.prev());
        $parent.remove();
    },
    /**
     * 右移
     */
    moveright: function (_this) {
        var $parent = $(_this).parent().parent();
        if (0 == $parent.next('.picupload').length) {
            art.dialog.tips('已经是最后一张！', 1);
            return;
        }
        $parent.clone().insertAfter($parent.next());
        $parent.remove();
    },
};
var MAXPICNAME = 20;
$(document).on("mouseover mouseout", ".thumbnail span", function (event) {
    if (event.type == "mouseover") {
        var val = $(this).attr('val');
        if (val && val.length > 20) {
            $(this).text(val);
        }
    } else if (event.type == "mouseout") {
        var val = $(this).attr('val');
        if (val && val.length > 20) {
            $(this).text(val.substring(0, MAXPICNAME) + '...');
        }
    }
});
window.picUpload = function (_this, dir, url=null, ext=null) {

    var d = dialog({
        title: '提示信息',
        width: 140,
        height: 70,
        buttons: {
            '确定': function () {
                $(this).dialog("close");
            }
        }
    });
    var $imgThis = $(_this).prevAll('img');
    var $ori_src = $imgThis.attr('src');
    var file_input_id = _this.id;
    var this_span = $(_this).prev().prev();
    $imgThis.attr('src', '/static/images/waiting.gif');
    if (url == null) {
        url = '/file/uploadPic?dir=' + dir + '&src_path=1';
    }
    $.ajaxFileUpload({
        url: url,
        secureuri: false,//是否需要安全协议
        fileElementId: _this.id, //文件上传域的ID
        dataType: 'json', //返回值类型 一般设置为json
        success: function (data, status) {  //服务器成功响应处理函数
            if ('succ' == data.stat) {
                var new_src_path = data.src_path.replace("protected", "general");
                var name = _this.value.substr(_this.value.lastIndexOf('\\') + 1);
                $('#pic_realname').val(name);
                //更改之前的图片的name和value
                $('#' + file_input_id).prev().val(data.path).attr('name', 'pic[]');
                if (ext != null) {
                    $("input[name='" + ext + "']").val(data.path);
                }
                if (name.length > 20) {
                    this_span.text(name.substring(0, MAXPICNAME) + '...');
                } else {
                    this_span.text(name);
                }
                this_span.attr('val', name);
                $imgThis.attr('src', new_src_path);
                return;
            }
            if (undefined != data.msg) {
                d.content(data.msg);
                d.showModal();
                $imgThis.attr('src', $ori_src);
            }
        },
        error: function (data, status, e) {//服务器响应失败处理函数
            d.content('上传图片失败，请刷新页面再试');
            d.showModal();
            $imgThis.attr('src', $ori_src);
        }
    });
};